package cocurrent;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class VolatileByRefTest {
    //这里如果不加volatile  线程2改变了值，线程1检测不到这个变化
    public List list = new ArrayList();

    @Test
    public void test() throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (list.isEmpty()) {

            }
//            System.out.println("thread1 out~~");

        });
        thread.start();

        Thread thread2 = new Thread(() -> {
//            try {
//                TimeUnit.SECONDS.sleep(1);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            list.add(new Object());
//            System.out.println("thread2 out~~");
        });


        thread2.start();

        thread.join();
        thread2.join();

    }
}
